package cn.ava.order.service.impl;

import cn.ava.order.domain.OrderPetAcquisition;
import cn.ava.order.dot.confirmOrderDto;
import cn.ava.order.mapper.OrderPetAcquisitionMapper;
import cn.ava.order.service.IOrderPetAcquisitionService;
import cn.ava.basic.service.impl.BaseServiceImpl;
import cn.ava.pet.domain.Pet;
import cn.ava.pet.domain.SearchMasterMsg;
import cn.ava.pet.mapper.PetMapper;
import cn.ava.pet.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: OrderPetAcquisitionServiceImpl
 * @Description: 宠物订单业务层处理
 * @Author: Avavaava
 * @Date: 2022/7/26 16:27
 * @version: 23.53
 *
 */
@Service
// 开启事务 只读属性和支持事务
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired // 宠物订单数据层
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;

    @Autowired // 寻主消息数据层
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired // 宠物信息数据层
    private PetMapper petMapper;

    /**
     * @Title: cancelOrder
     * @Description: 拒绝订单处理
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/26 16:29
     * @param:
     * @return:
     */
    @Override
    @Transactional
    public void cancelOrder(Long id) {
        // 查询出对应的订单信息
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.getById(id);
        // 设置状态为拒绝
        orderPetAcquisition.setState(-1);
        // 修改数据
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        // 对寻主消息作处理,查询出对应的消息数据对象
        SearchMasterMsg msg = searchMasterMsgMapper.getById(orderPetAcquisition.getSearchMasterMsgId());
        // 设置shopId为空,放入寻主池
        msg.setShopId(null);
        // 设置状态为通过
        msg.setState(1);
        // 修改寻主消息数据
        searchMasterMsgMapper.update(msg);
        // 拒绝订单,删除对应的宠物信息
        petMapper.delete(orderPetAcquisition.getPetId());
    }


    /**
     * @Title: confirmOrder
     * @Description: 确认订单处理
     * @Author: Avavaava
     * @Version: 23.53
     * @Date: 2022/7/26 16:29
     * @param:
     * @return:
     */
    @Override
    @Transactional
    public void confirmOrder(confirmOrderDto confirmOrderDto) {
        // 1.根据订单Id查询订单，修改订单信息
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.getById(confirmOrderDto.getId());
        // 设置状态为完成
        orderPetAcquisition.setState(1);
        // 设置订单金额
        orderPetAcquisition.setPrice(confirmOrderDto.getMoney());
        // 支付方式
        orderPetAcquisition.setPaytype(orderPetAcquisition.getPaytype());
        // 修改订单信息
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        // 修改寻主消息状态
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.getById(orderPetAcquisition.getSearchMasterMsgId());
        // 修改寻主消息为已完成
        searchMasterMsg.setState(2);
        // 修改数据
        searchMasterMsgMapper.update(searchMasterMsg);
        // 修改宠物基本信息，状态
        Pet pet = petMapper.getById(orderPetAcquisition.getPetId());
        pet.setCostprice(confirmOrderDto.getMoney());
        // 修改数据
        petMapper.update(pet);
    }
}
